package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.RoleBean;
import com.woniuxy.educationplatform.service.IRoleService;
import com.woniuxy.educationplatform.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author unreleased
 * @date 2020/12/13 10:50
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IRoleService roleServiceImpl;

    /**
     * 查询所有状态正常的角色
     * @return 返回result工具类对象
     */
    @RequestMapping("/findAll")
    public ResultUtil findAllRoles(){
        List<RoleBean> roles = roleServiceImpl.findAllRoles();
        ResultUtil<List<RoleBean>> resultUtil = new ResultUtil<>();
        if (roles == null) {
            resultUtil.setCode(ResultUtil.ERROR).setMessage("出错了…… 没有找到");
        }else {
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("请求成功").setData(roles);
        }
        return resultUtil;
    }
    @RequestMapping("/findList")
    public ResultUtil findList(){
        List<RoleBean> roleList = roleServiceImpl.findRoleList();
        ResultUtil<List<RoleBean>> resultUtil = new ResultUtil<>();
        if (roleList == null) {
            resultUtil.setCode(ResultUtil.ERROR).setMessage("出错了…… 没有找到");
        }else {
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("success").setData(roleList);
        }
        return resultUtil;
    }
    @PutMapping("/updateMsg")
    public ResultUtil updateRoleById(RoleBean roleBean) {
        System.out.println("roleBean = " + roleBean);
        ResultUtil resultUtil = new ResultUtil();
        if(roleServiceImpl.updateRoleById(roleBean)){
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("修改成功");
        }else {
            return resultUtil.setCode(ResultUtil.ERROR).setMessage("修改失败");
        }
    }
    @DeleteMapping("/del/{id}")
    public ResultUtil delRoleById(@PathVariable Integer id){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if(roleServiceImpl.delRoleById(id)){
            resultUtil.setCode(ResultUtil.SUCCESS).setMessage("删除成功");
        }
        return  resultUtil;
    }
    @PostMapping("/saveRole")
    public ResultUtil saveAdmin (RoleBean roleBean){
        ResultUtil resultUtil = new ResultUtil().setCode(ResultUtil.ERROR).setMessage("系统繁忙，请稍后再试");
        if(roleServiceImpl.saveRole(roleBean)){
            return resultUtil.setCode(ResultUtil.SUCCESS).setMessage("添加成功");
        }
        return resultUtil;
    }

}
